Add some tests for Repo
authorFelix Krull <f_krull@gmx.de>
Sat, 18 May 2019 14:13:54 +0000 (16:13 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:53 +0000 (12:53 -0400)
rust-bindings/rust/src/repo.rs [deleted file]
rust-bindings/rust/src/repo/mod.rs [new file with mode: 0644]
rust-bindings/rust/src/repo/tests.rs [new file with mode: 0644]

diff --git a/rust-bindings/rust/src/repo.rs b/rust-bindings/rust/src/repo.rs
deleted file mode 100644 (file)
index 5ed8378..0000000
+++ /dev/null
@@ -1,142 +0,0 @@
-use crate::auto::Repo;
-#[cfg(any(feature = "v2016_4", feature = "dox"))]
-use crate::auto::RepoListRefsExtFlags;
-use ffi;
-use gio;
-use glib;
-use glib::translate::*;
-use glib::Error;
-use glib::IsA;
-use glib_ffi;
-use std::collections::{HashMap, HashSet};
-use std::path::Path;
-use std::ptr;
-use ObjectName;
-
-unsafe extern "C" fn read_variant_table(
-    _key: glib_ffi::gpointer,
-    value: glib_ffi::gpointer,
-    hash_set: glib_ffi::gpointer,
-) {
-    let value: glib::Variant = from_glib_none(value as *const glib_ffi::GVariant);
-    let set: &mut HashSet<ObjectName> = &mut *(hash_set as *mut HashSet<ObjectName>);
-    set.insert(ObjectName::new_from_variant(value));
-}
-
-unsafe fn from_glib_container_variant_set(ptr: *mut glib_ffi::GHashTable) -> HashSet<ObjectName> {
-    let mut set = HashSet::new();
-    glib_ffi::g_hash_table_foreach(
-        ptr,
-        Some(read_variant_table),
-        &mut set as *mut HashSet<ObjectName> as *mut _,
-    );
-    glib_ffi::g_hash_table_unref(ptr);
-    set
-}
-
-pub trait RepoExtManual {
-    fn new_for_path<P: AsRef<Path>>(path: P) -> Repo;
-
-    fn traverse_commit<'a, P: Into<Option<&'a gio::Cancellable>>>(
-        &self,
-        commit_checksum: &str,
-        maxdepth: i32,
-        cancellable: P,
-    ) -> Result<HashSet<ObjectName>, Error>;
-
-    fn list_refs<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
-        &self,
-        refspec_prefix: P,
-        cancellable: Q,
-    ) -> Result<HashMap<String, String>, Error>;
-
-    #[cfg(any(feature = "v2016_4", feature = "dox"))]
-    fn list_refs_ext<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
-        &self,
-        refspec_prefix: P,
-        flags: RepoListRefsExtFlags,
-        cancellable: Q,
-    ) -> Result<HashMap<String, String>, Error>;
-}
-
-impl<O: IsA<Repo> + IsA<glib::Object> + Clone + 'static> RepoExtManual for O {
-    fn new_for_path<P: AsRef<Path>>(path: P) -> Repo {
-        Repo::new(&gio::File::new_for_path(path.as_ref()))
-    }
-
-    fn traverse_commit<'a, P: Into<Option<&'a gio::Cancellable>>>(
-        &self,
-        commit_checksum: &str,
-        maxdepth: i32,
-        cancellable: P,
-    ) -> Result<HashSet<ObjectName>, Error> {
-        unsafe {
-            let mut error = ptr::null_mut();
-            let mut hashtable = ptr::null_mut();
-            let _ = ffi::ostree_repo_traverse_commit(
-                self.to_glib_none().0,
-                commit_checksum.to_glib_none().0,
-                maxdepth,
-                &mut hashtable,
-                cancellable.into().to_glib_none().0,
-                &mut error,
-            );
-            if error.is_null() {
-                Ok(from_glib_container_variant_set(hashtable))
-            } else {
-                Err(from_glib_full(error))
-            }
-        }
-    }
-
-    fn list_refs<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
-        &self,
-        refspec_prefix: P,
-        cancellable: Q,
-    ) -> Result<HashMap<String, String>, Error> {
-        unsafe {
-            let mut error = ptr::null_mut();
-            let mut hashtable = ptr::null_mut();
-            let _ = ffi::ostree_repo_list_refs(
-                self.to_glib_none().0,
-                refspec_prefix.into().to_glib_none().0,
-                &mut hashtable,
-                cancellable.into().to_glib_none().0,
-                &mut error,
-            );
-
-            if error.is_null() {
-                Ok(FromGlibPtrContainer::from_glib_container(hashtable))
-            } else {
-                Err(from_glib_full(error))
-            }
-        }
-    }
-
-    #[cfg(any(feature = "v2016_4", feature = "dox"))]
-    fn list_refs_ext<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
-        &self,
-        refspec_prefix: P,
-        flags: RepoListRefsExtFlags,
-        cancellable: Q,
-    ) -> Result<HashMap<String, String>, Error> {
-        unsafe {
-            let mut error = ptr::null_mut();
-            let mut hashtable = ptr::null_mut();
-            let _ = ffi::ostree_repo_list_refs_ext(
-                self.to_glib_none().0,
-                refspec_prefix.into().to_glib_none().0,
-                &mut hashtable,
-                flags.to_glib(),
-                cancellable.into().to_glib_none().0,
-                &mut error,
-            );
-
-            if error.is_null() {
-                Ok(FromGlibPtrContainer::from_glib_container(hashtable))
-            } else {
-                Err(from_glib_full(error))
-            }
-        }
-    }
-}
diff --git a/rust-bindings/rust/src/repo/mod.rs b/rust-bindings/rust/src/repo/mod.rs
new file mode 100644 (file)
index 0000000..1245496
--- /dev/null
@@ -0,0 +1,145 @@
+use crate::auto::Repo;
+#[cfg(any(feature = "v2016_4", feature = "dox"))]
+use crate::auto::RepoListRefsExtFlags;
+use ffi;
+use gio;
+use glib;
+use glib::translate::*;
+use glib::Error;
+use glib::IsA;
+use glib_ffi;
+use std::collections::{HashMap, HashSet};
+use std::path::Path;
+use std::ptr;
+use ObjectName;
+
+unsafe extern "C" fn read_variant_table(
+    _key: glib_ffi::gpointer,
+    value: glib_ffi::gpointer,
+    hash_set: glib_ffi::gpointer,
+) {
+    let value: glib::Variant = from_glib_none(value as *const glib_ffi::GVariant);
+    let set: &mut HashSet<ObjectName> = &mut *(hash_set as *mut HashSet<ObjectName>);
+    set.insert(ObjectName::new_from_variant(value));
+}
+
+unsafe fn from_glib_container_variant_set(ptr: *mut glib_ffi::GHashTable) -> HashSet<ObjectName> {
+    let mut set = HashSet::new();
+    glib_ffi::g_hash_table_foreach(
+        ptr,
+        Some(read_variant_table),
+        &mut set as *mut HashSet<ObjectName> as *mut _,
+    );
+    glib_ffi::g_hash_table_unref(ptr);
+    set
+}
+
+pub trait RepoExtManual {
+    fn new_for_path<P: AsRef<Path>>(path: P) -> Repo;
+
+    fn traverse_commit<'a, P: Into<Option<&'a gio::Cancellable>>>(
+        &self,
+        commit_checksum: &str,
+        maxdepth: i32,
+        cancellable: P,
+    ) -> Result<HashSet<ObjectName>, Error>;
+
+    fn list_refs<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
+        &self,
+        refspec_prefix: P,
+        cancellable: Q,
+    ) -> Result<HashMap<String, String>, Error>;
+
+    #[cfg(any(feature = "v2016_4", feature = "dox"))]
+    fn list_refs_ext<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
+        &self,
+        refspec_prefix: P,
+        flags: RepoListRefsExtFlags,
+        cancellable: Q,
+    ) -> Result<HashMap<String, String>, Error>;
+}
+
+impl<O: IsA<Repo> + IsA<glib::Object> + Clone + 'static> RepoExtManual for O {
+    fn new_for_path<P: AsRef<Path>>(path: P) -> Repo {
+        Repo::new(&gio::File::new_for_path(path.as_ref()))
+    }
+
+    fn traverse_commit<'a, P: Into<Option<&'a gio::Cancellable>>>(
+        &self,
+        commit_checksum: &str,
+        maxdepth: i32,
+        cancellable: P,
+    ) -> Result<HashSet<ObjectName>, Error> {
+        unsafe {
+            let mut error = ptr::null_mut();
+            let mut hashtable = ptr::null_mut();
+            let _ = ffi::ostree_repo_traverse_commit(
+                self.to_glib_none().0,
+                commit_checksum.to_glib_none().0,
+                maxdepth,
+                &mut hashtable,
+                cancellable.into().to_glib_none().0,
+                &mut error,
+            );
+            if error.is_null() {
+                Ok(from_glib_container_variant_set(hashtable))
+            } else {
+                Err(from_glib_full(error))
+            }
+        }
+    }
+
+    fn list_refs<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
+        &self,
+        refspec_prefix: P,
+        cancellable: Q,
+    ) -> Result<HashMap<String, String>, Error> {
+        unsafe {
+            let mut error = ptr::null_mut();
+            let mut hashtable = ptr::null_mut();
+            let _ = ffi::ostree_repo_list_refs(
+                self.to_glib_none().0,
+                refspec_prefix.into().to_glib_none().0,
+                &mut hashtable,
+                cancellable.into().to_glib_none().0,
+                &mut error,
+            );
+
+            if error.is_null() {
+                Ok(FromGlibPtrContainer::from_glib_container(hashtable))
+            } else {
+                Err(from_glib_full(error))
+            }
+        }
+    }
+
+    #[cfg(any(feature = "v2016_4", feature = "dox"))]
+    fn list_refs_ext<'a, 'b, P: Into<Option<&'a str>>, Q: Into<Option<&'b gio::Cancellable>>>(
+        &self,
+        refspec_prefix: P,
+        flags: RepoListRefsExtFlags,
+        cancellable: Q,
+    ) -> Result<HashMap<String, String>, Error> {
+        unsafe {
+            let mut error = ptr::null_mut();
+            let mut hashtable = ptr::null_mut();
+            let _ = ffi::ostree_repo_list_refs_ext(
+                self.to_glib_none().0,
+                refspec_prefix.into().to_glib_none().0,
+                &mut hashtable,
+                flags.to_glib(),
+                cancellable.into().to_glib_none().0,
+                &mut error,
+            );
+
+            if error.is_null() {
+                Ok(FromGlibPtrContainer::from_glib_container(hashtable))
+            } else {
+                Err(from_glib_full(error))
+            }
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests;
diff --git a/rust-bindings/rust/src/repo/tests.rs b/rust-bindings/rust/src/repo/tests.rs
new file mode 100644 (file)
index 0000000..de38812
--- /dev/null
@@ -0,0 +1,14 @@
+use super::*;
+use crate::RepoMode;
+
+#[test]
+fn should_get_repo_mode_from_string() {
+    let mode = Repo::mode_from_string("archive").unwrap();
+    assert_eq!(RepoMode::Archive, mode);
+}
+
+#[test]
+fn should_return_error_for_invalid_repo_mode_string() {
+    let result = Repo::mode_from_string("invalid-repo-mode");
+    assert!(result.is_err());
+}
\ No newline at end of file